package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    //根据id查询品牌
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> BasefindByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(ids)));
    }
    //根据品牌id查询品牌集合
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> BasefindById(@RequestParam("id")Long id){
        return ResponseEntity.ok(brandService.queryBrandByCategory(id));
    }

    //根据id查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO>BasefindByIdlist(@PathVariable("id")Long id){
    return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }

    //分页查询品牌
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>>queryByPaGe(@RequestParam(value = "key", required = false)String key, @RequestParam(value = "page", defaultValue = "1")Integer page, @RequestParam(value = "rows", defaultValue = "1")Integer rows){
        return ResponseEntity.ok(brandService.queryByPaGe(key,page,rows));
    }

    //添加品牌
    @PostMapping
    public ResponseEntity<Void>Brandadd(BrandDTO brandDTO){
        brandService.add(brandDTO);
    return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //删除品牌
    @DeleteMapping("/{id}")
    public ResponseEntity<Void>deleteById(@PathVariable("id") Long id){
        brandService.deleteById(id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //更新品牌
    @PutMapping
    public ResponseEntity<Void>updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


}
